package BanyuanClub.Work.项目;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.io.*;
import java.net.Socket;

public class CharkClient {

    private JFrame frame;
    JTextArea textArea = new JTextArea();
    JTextField textField;
    JTextField textField_1;
    LinkSet dialog;
    UserSet dialog1;
    JLabel lblNewLabel;
    JComboBox<String> comboBox;
    JButton btnNewButton_2;
    private String IP = "127.0.0.1";
    private int portNumber;
    private String userName;
    private ObjectOutputStream writer;


    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                CharkClient window = new CharkClient();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }


    //链接服务器
    Socket  socket;
    CharkServer charkServer = new CharkServer();
    public void serverLink(){
        if (userName == null){
            textArea.append("请先设置用户名\n");
        }else if (portNumber == 0){
            textArea.append("请先设置端口号\n");
        }else {
            new Thread(() -> {
                try {
                    socket = new Socket(IP, portNumber);
                        textArea.append("登录成功\n");
                        btnNewButton_2.setEnabled(false);
                        textField_1.setText(charkServer.getUsers().size() + "人");
                        writer = new ObjectOutputStream(new FileOutputStream(String.valueOf(socket.getOutputStream())));
                        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                        writer.writeObject(userName);
                        writer.writeObject(userName + "#" + "我上线了");
                        while (true) {
                            String st = reader.readLine();
                            if (st != null) {
                               textArea.append(st + "\n");
                            }
                        }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

    /**
     * Create the application.
     */
    public CharkClient() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 640, 455);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        //链接设置窗口
        dialog = new LinkSet(frame);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(false);

        //用户设置窗口
        dialog1 = new UserSet(frame);
        dialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog1.setVisible(false);

        //标签
        lblNewLabel = new JLabel("客户端");
        lblNewLabel.setForeground(Color.BLUE);
        lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 15));
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setBounds(6, 6, 89, 30);
        frame.getContentPane().add(lblNewLabel);

        //用户设置按钮
        JButton btnNewButton = new JButton("用户设置");
        btnNewButton.addActionListener(e -> dialog1.setVisible(true));
        btnNewButton.setBounds(104, 9, 117, 29);
        frame.getContentPane().add(btnNewButton);

        //链接设置按钮
        JButton btnNewButton_1 = new JButton("链接设置");
        btnNewButton_1.addActionListener(e -> dialog.setVisible(true));
        btnNewButton_1.setBounds(223, 9, 117, 29);
        frame.getContentPane().add(btnNewButton_1);

        //登录按钮
        btnNewButton_2 = new JButton("登录");
        btnNewButton_2.setBounds(362, 9, 117, 29);
        frame.getContentPane().add(btnNewButton_2);
        btnNewButton_2.addActionListener(e -> serverLink());

        //退出按钮
        JButton btnNewButton_3 = new JButton("退出");
        btnNewButton_3.setBounds(480, 9, 117, 29);
        frame.getContentPane().add(btnNewButton_3);
        btnNewButton_3.addActionListener(e -> {
            try {
                writer.writeObject(userName + "#" + "我先溜了！");
                socket.close();
                System.exit(0);
            } catch (IOException ioException) {
                ioException.printStackTrace();
            }
        });

        //文本域
        textArea.setBounds(31, 48,575, 266);
        frame.getContentPane().add(textArea);

        //标签
        JLabel lblNewLabel_1 = new JLabel("发送消息：");
        lblNewLabel_1.setFont(new Font("Lucida Grande", Font.BOLD, 13));
        lblNewLabel_1.setBounds(31, 384, 65, 26);
        frame.getContentPane().add(lblNewLabel_1);

        //标签
        JLabel lblNewLabel_2 = new JLabel("发送至：");
        lblNewLabel_2.setFont(new Font("Lucida Grande", Font.BOLD, 13));
        lblNewLabel_2.setBounds(31, 345, 61, 16);
        frame.getContentPane().add(lblNewLabel_2);

        //标签
        JButton btnNewButton_4 = new JButton("发送");
        btnNewButton_4.setBounds(516, 383, 81, 30);
        frame.getContentPane().add(btnNewButton_4);
        btnNewButton_4.addActionListener(e -> {
            if (!textField.getText().equals("")) {
                try {
                    writer.writeObject(userName + "#" + textField.getText());
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
                textField.setText("");
            }
        });

        //复选框
        comboBox = new JComboBox<String>();
        comboBox.setModel(new DefaultComboBoxModel<>());
        comboBox.setBounds(97, 341, 95, 27);
        frame.getContentPane().add(comboBox);
        comboBox.addItem("所有人");

        //输入框
        textField = new JTextField();
        textField.setBounds(97, 380, 413, 35);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        //标签
        JLabel lblNewLabel_3 = new JLabel("在线人数：");
        lblNewLabel_3.setFont(new Font("Lucida Grande", Font.BOLD, 13));
        lblNewLabel_3.setBounds(480, 345, 65, 16);
        frame.getContentPane().add(lblNewLabel_3);

        //在线人数显示框
        textField_1 = new JTextField();
        textField_1.setBackground(SystemColor.window);
        textField_1.setBounds(545, 338, 61, 30);
        frame.getContentPane().add(textField_1);
        textField_1.setColumns(10);
//        textField_1.setText(charkServer.getIndex()+"人");
    }

    //链接设置类
    public class LinkSet extends JDialog {

        private final JTextField textField;
        private final JTextField textField_1;


        /**
         * Create the dialog.
         */
        public LinkSet(JFrame jframe) {
            super(jframe);
            setBounds(100, 100, 388, 228);
            getContentPane().setLayout(new BorderLayout());
            JPanel contentPanel = new JPanel();
            contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            getContentPane().add(contentPanel, BorderLayout.CENTER);
            contentPanel.setLayout(null);
            {
                JLabel lblNewLabel = new JLabel("IP地址：");
                lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
                lblNewLabel.setBounds(39, 40, 51, 16);
                contentPanel.add(lblNewLabel);
            }
            {
                JLabel lblNewLabel_2 = new JLabel("IP默认为127.0.0.1");
                lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
                lblNewLabel_2.setBounds(112, 115, 162, 33);
                contentPanel.add(lblNewLabel_2);
            }
            {
                JLabel lblNewLabel_1 = new JLabel("端口号：");
                lblNewLabel_1.setFont(new Font("Lucida Grande", Font.BOLD, 13));
                lblNewLabel_1.setBounds(39, 83, 61, 16);
                contentPanel.add(lblNewLabel_1);
            }
            {
                textField = new JTextField();
                textField.setBounds(102, 28, 212, 33);
                contentPanel.add(textField);
                textField.setColumns(10);
            }
            {
                textField_1 = new JTextField();
                textField_1.setBounds(102, 71, 212, 33);
                contentPanel.add(textField_1);
                textField_1.setColumns(10);

            }
            {
                JPanel buttonPane = new JPanel();
                buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
                getContentPane().add(buttonPane, BorderLayout.SOUTH);
                {
                    JButton okButton = new JButton("确定");
                    okButton.setActionCommand("OK");
                    buttonPane.add(okButton);
                    getRootPane().setDefaultButton(okButton);
                    okButton.addActionListener((e)->{
                        IP = textField.getText();
                        portNumber = Integer.parseInt(textField_1.getText());
                        dialog.setVisible(false);
                    });
                }
                {
                    JButton cancelButton = new JButton("取消");
                    cancelButton.setActionCommand("Cancel");
                    buttonPane.add(cancelButton);
                    cancelButton.addActionListener((e)->{
                        textArea.append("IP地址和端口号设置已取消\n");
                        dialog.setVisible(false);
                    });
                }
            }
        }

    }

    //用户设置类
    public class UserSet extends JDialog {

        private final JTextField textField;

        /**
         * Create the dialog.
         */
        public UserSet(JFrame jframe) {
            super(jframe);
            setBounds(100, 100, 388, 228);
            getContentPane().setLayout(new BorderLayout());
            JPanel contentPanel = new JPanel();
            contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            getContentPane().add(contentPanel, BorderLayout.CENTER);
            contentPanel.setLayout(null);
            {
                JLabel lblNewLabel = new JLabel("请输入用户名：");
                lblNewLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
                lblNewLabel.setBounds(54, 42, 98, 24);
                contentPanel.add(lblNewLabel);
            }
            {
                textField = new JTextField();
                textField.setBounds(105, 78, 172, 33);
                contentPanel.add(textField);
                textField.setColumns(10);
            }
            {
                JPanel buttonPane = new JPanel();
                buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
                getContentPane().add(buttonPane, BorderLayout.SOUTH);
                {
                    JButton okButton = new JButton("确定");
                    okButton.setActionCommand("OK");
                    buttonPane.add(okButton);
                    getRootPane().setDefaultButton(okButton);
                    okButton.addActionListener((e)->{
                        userName = textField.getText();
                        lblNewLabel.setText(userName);
                        dialog1.setVisible(false);
                    });
                }
                {
                    JButton cancelButton = new JButton("取消");
                    cancelButton.setActionCommand("Cancel");
                    buttonPane.add(cancelButton);
                    cancelButton.addActionListener((e) ->{
                        textArea.append("用户名设置已取消\n");
                        dialog1.setVisible(false);
                    });
                }
            }
        }

    }


}
